<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="问题id" prop="problemId">
      <el-input v-model="dataForm.problemId" placeholder="问题id"></el-input>
    </el-form-item>
    <el-form-item label="问题答案" prop="answer">
      <el-input v-model="dataForm.answer" placeholder="问题答案"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="修改人" prop="updateUser">
      <el-input v-model="dataForm.updateUser" placeholder="修改人"></el-input>
    </el-form-item>
    <el-form-item label="删除标识(1-未删除,-1已删除)" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder="删除标识(1-未删除,-1已删除)"></el-input>
    </el-form-item>
    <el-form-item label="租户Id" prop="tenantId">
      <el-input v-model="dataForm.tenantId" placeholder="租户Id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getWxProblemAnswerObj, addWxProblemAnswerObj, putWxProblemAnswerObj} from '@/api/mp/wxproblemanswer'

    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          problemId: '',
          answer: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: '',
          tenantId: ''
        },
        dataRule: {
          problemId: [
            { required: true, message: '问题id不能为空', trigger: 'blur' }
          ],
          answer: [
            { required: true, message: '问题答案不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '删除标识(1-未删除,-1已删除)不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户Id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxProblemAnswerObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putWxProblemAnswerObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addWxProblemAnswerObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
